package cn.wolfcode.wolf2w.business.controller;


import cn.wolfcode.wolf2w.business.api.domain.*;
import cn.wolfcode.wolf2w.business.query.SearchQuery;
import cn.wolfcode.wolf2w.business.service.ISearchService;
import cn.wolfcode.wolf2w.common.core.constant.SecurityConstants;
import cn.wolfcode.wolf2w.common.core.domain.R;
import cn.wolfcode.wolf2w.member.api.domain.UserInfo;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageImpl;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/search")
@RestController
public class SearchController {
    @Autowired
    private ISearchService searchService;

    @PostMapping("/initDestinationEs")
    public R<?> initDestinationEs(@RequestBody DestinationEs destinationEs){
        searchService.saveDestination(destinationEs);
        return R.ok();
    }


    //存储到ES中
    @PostMapping("/initStrategyEs")
    public R<?> initStrategyEs(@RequestBody StrategyEs strategyEs){
        searchService.saveStrategy(strategyEs);
        return R.ok();
    }

    @PostMapping("/UserEs")
    public R<?> UserEs(@RequestBody UserEs userInfoEs){
        searchService.saveUserInfo(userInfoEs);
        return R.ok();
    }

    @PostMapping("/initNoteEs")
    public R<?> initNoteEs(@RequestBody NoteEs noteEs){
        searchService.saveNote(noteEs);
        return R.ok();
    }

    //对外同一接口，对内分开处理
    @GetMapping
    public R<?> search(SearchQuery qo) throws InvocationTargetException, IllegalAccessException {
        switch (qo.getType()) {
            //查所有
            case 1:
                return R.ok(searchStrategy(qo));
            case 2:
                return R.ok(searchNote(qo));
            case 3:
                return R.ok(searchUser(qo));
            case 4:
                return R.ok(searchAnswer(qo));
            default:
                return R.ok(searchAll(qo));
        }
    }
    private Object searchAll(SearchQuery qo) throws InvocationTargetException, IllegalAccessException {
        PageImpl<UserInfo> userPage= searchService.searchWithHightLight(UserInfo.class,UserEs.class,qo,"info", "nickname","city");
        PageImpl<Note> notePage=searchService.searchWithHightLight(Note.class,NoteEs.class,qo,"title", "summary");
        PageImpl<Strategy> strategyPage=searchService.searchWithHightLight(Strategy.class,StrategyEs.class,qo,"title","subTitle","summary");
        PageImpl<Destination> destinationPage=searchService.searchWithHightLight(Destination.class,DestinationEs.class,qo,"name", "info");
        long total = userPage.getTotalElements()+notePage.getTotalElements()+strategyPage.getTotalElements()+destinationPage.getTotalElements();
        List<UserInfo> users = userPage.getContent();
        List<Note> notes = notePage.getContent();
        List<Strategy> strategies = strategyPage.getContent();
        List<Destination> dests = destinationPage.getContent();
        Map map=new HashMap();
        map.put("users",users);
        map.put("notes",notes);
        map.put("strategies",strategies);
        map.put("dests",dests);
        map.put("total",total);
        return map;
    }
    private Object searchAnswer(SearchQuery qo) {
        return null;
    }
    private Object searchUser(SearchQuery qo) throws InvocationTargetException, IllegalAccessException {
//        return searchService.searchWithHightLightAndUser(Strategy.class,StrategyEs.class,qo,"info", "nickname","city");
        return searchService.searchWithHightLight(UserInfo.class,UserEs.class,qo,"info", "nickname","city");
    }
    private Object searchNote(SearchQuery qo) throws InvocationTargetException, IllegalAccessException {
        return searchService.searchWithHightLight(Note.class,NoteEs.class,qo,"title", "summary");
    }
    private Object searchStrategy(SearchQuery qo) throws InvocationTargetException, IllegalAccessException {
        return searchService.searchWithHightLight(Strategy.class,StrategyEs.class,qo,"title","subTitle","summary");
    }
}
